package model

import (
	"gorm.io/gorm"
	"time"
)

type TProduct struct {
	gorm.Model

	Name        string `json:"name" gorm:"type:varchar(255);comment:名称"`
	MainPic     string `json:"mainPic" gorm:"type:varchar(255);comment:产品主图"`
	Sold        uint16 `json:"sold" gorm:"type:int(4);comment:售出数量"`
	Description string `json:"description" gorm:"type:longtext;comment:产品描述"`
	CommonPrice uint16 `json:"commonPrice" gorm:"type:smallint(5) unsigned;comment:价格"`
	MemberPrice uint16 `json:"memberPrice" gorm:"type:smallint(5) unsigned;comment:价格"`
	VipPrice    uint16 `json:"vipPrice" gorm:"type:smallint(5) unsigned;comment:价格"`

	DownloadUrl  string `gorm:"size:255;comment:下载地址"`
	Pwd          string `gorm:"size:32;comment:下载密码"`
	DownloadType uint8  `gorm:"comment:下载类型，1：百度"`

	FinishPicDown bool       `gorm:"comment:是否完成图片下载"`
	UpdatedDay    *time.Time `gorm:"comment:外部平台更新时间"`
}

func (t *TProduct) TableName() string {
	return "t_product"
}

func (r *TProduct) Create(product *TProduct) error {
	return GetDB().Create(product).Error
}

func (r *TProduct) Update(product *TProduct) error {
	return GetDB().Save(product).Error
}

func (r *TProduct) FirstById(id uint64) (*TProduct, error) {
	var product TProduct
	err := GetDB().Where("id = ?", id).First(&product).Error
	return &product, err
}
